﻿@inherits ClientSectionCompontentBase

<MRow>
    <MCol Md=12 Class="text-center">
        <UploadAvatar @bind-Value="Dto.LogoUri" Avatar Size="80" WhenFileChangeUpload />
    </MCol>
    <MCol Md=6>
        <STextField Required @bind-Value="Dto.ClientId" Label="@T("ClientId")" Tooltip="@T("ClientIdTips")" />
    </MCol>
    <MCol Md=6>
        <STextField Required @bind-Value="Dto.ClientName" Label="@T("ClientName")" Tooltip="@T("ClientNameTips")" />
    </MCol>
    <MCol Md=6>
        <SUrlTextField @bind-Value="Dto.ClientUri" Label="@T("ClientURL")" />
    </MCol>
    <MCol Md=6 Class="d-flex justify-space-around">
        <MCheckbox @bind-Value="Dto.RequireConsent" Label="@T("RequireConsent")" TValue="bool"/>
        <MCheckbox @bind-Value="Dto.AllowOfflineAccess" Label="@T("AllowOfflineAccess")" TValue="bool"/>
    </MCol>
</MRow>
<MRow>
    <MCol Md=6>
        <div class="block-center">
            <STextField @bind-Value="Dto.RedirectUri" Label="@T("CallBackRedirectUri")" Suffix="@redirectUriSuffix" />
            <SButton Icon OnClick="AddCallBackRedirectUri" Color="regular3">
                <SIcon>@IconConstants.Add</SIcon>
            </SButton>
        </div>
        @foreach (var uri in Dto.RedirectUris)
        {
            <div class="d-flex mt-3">
                <STextField Small HideDetails="true" Value="uri" Filled Disabled />
                <SButton Icon OnClick="()=>RemoveCallBackRedirectUri(uri)" Color="regular3">
                    <SIcon Tooltip="@T("Delete")">mdi-close-circle</SIcon>
                </SButton>
            </div>
        }
    </MCol>
    <MCol Md=6>
        <div class="block-center">
            <STextField @bind-Value="Dto.PostLogoutRedirectUri" Label="@T("PostLogoutRedirectUri")" Suffix="@logoutRedirectUriSuffix" />
            <SButton Icon OnClick="AddLogoutRedirectUri" Color="regular3">
                <SIcon>@IconConstants.Add</SIcon>
            </SButton>
        </div>
        @foreach (var uri in Dto.PostLogoutRedirectUris)
        {
            <div class="d-flex mt-3">
                <STextField Small HideDetails="true" Value="uri" Filled Disabled />
                <SButton Icon OnClick="()=>RemoveLogoutRedirectUri(uri)" Color="regular3">
                    <SIcon Tooltip="@T("Delete")">mdi-close-circle</SIcon>
                </SButton>
            </div>
        }
    </MCol>
</MRow>
<MRow>
    <MCol Md=12>
        <STextField @bind-Value="Dto.Description" Label="@T("Description")" />
    </MCol>
</MRow>

@code {
    [Parameter]
    public AddClientBasicDto Dto { get; set; } = new();

    [Parameter]
    public EventCallback<AddClientBasicDto> DtoChanged { get; set; }

    readonly string redirectUriSuffix = "/signin-oidc";
    readonly string logoutRedirectUriSuffix = "/signout-callback-oidc";

    private void AddCallBackRedirectUri()
    {
        if (!Uri.TryCreate(Dto.RedirectUri, UriKind.Absolute, out var uri))
        {
            PopupService.EnqueueSnackbarAsync(T("Is not a valid URL address"), AlertTypes.Error);
            return;
        }
        var redirectUri = $"{Dto.RedirectUri.TrimEnd('/')}{redirectUriSuffix}".ToLower();
        if (Dto.RedirectUris.Contains(redirectUri))
        {
            PopupService.EnqueueSnackbarAsync(T("Duplicate item"), AlertTypes.Error);
            return;
        }
        Dto.RedirectUris.Add(redirectUri);
        Dto.RedirectUri = string.Empty;
    }

    private void AddLogoutRedirectUri()
    {
        if (!Uri.TryCreate(Dto.PostLogoutRedirectUri, UriKind.Absolute, out var uri))
        {
            PopupService.EnqueueSnackbarAsync(T("Is not a valid URL address"), AlertTypes.Error);
            return;
        }
        var postLogoutRedirectUri = $"{Dto.PostLogoutRedirectUri.TrimEnd('/')}{logoutRedirectUriSuffix}";
        if (Dto.PostLogoutRedirectUris.Contains(postLogoutRedirectUri))
        {
            PopupService.EnqueueSnackbarAsync(T("Duplicate item"), AlertTypes.Error);
            return;
        }
        Dto.PostLogoutRedirectUris.Add(postLogoutRedirectUri);
        Dto.PostLogoutRedirectUri = string.Empty;
    }

    private void RemoveCallBackRedirectUri(string val)
    {
        Dto.RedirectUris.Remove(val);
    }

    private void RemoveLogoutRedirectUri(string val)
    {
        Dto.PostLogoutRedirectUris.Remove(val);
    }
}
